<?php

/* 品牌 brand */
class BrandModel extends BaseModel
{
    var $table  = 'brand';
    var $prikey = 'brand_id';
    var $_name  = 'brand';
	
    var $_relation = array(
		'has_buyer' => array(
            'model'         => 'buyer',
            'type'          => HAS_MANY,
            'foreign_key'   => 'brand_id'
        ),
		'has_sample' => array(
			'model' => 'sample',
			'type' => HAS_MANY,
			'foreign_key' => 'brand_id'
		),
		'has_order' => array(
			'model' => 'order',
			'type' => HAS_MANY,
			'foreign_key' => 'brand_id'
		),
		'belong_company' => array(
			'model' => 'company',
			'type' => BELONGS_TO,
			'foreign_key' => 'company_id',
			'reverse'       => 'has_brand'
		),
    	'has_main_label' => array(
    		'model' => 'main_label',
    		'type' => HAS_MANY,
    		'foreign_key' => 'brand_id',
    	),
    	'has_listing' => array(
    		'model' => 'listing',
    		'type' => HAS_MANY,
    		'foreign_key' => 'brand_id',
    	)
	);

        /*
     * 判断名称是否唯一
     */
    function unique($brand_name,$brand_id=0, $company_id = 0)
    {
        $conditions = "brand_name = '" . $brand_name . "' AND company_id = ".$company_id ." AND brand_id !={$brand_id}";
        return count($this->find(array('conditions' => $conditions))) == 0;
    }
}

?>